package com.fxf.srb.core.controller.api;

import com.alibaba.fastjson.JSON;
import com.fxf.common.result.R;
import com.fxf.srb.base.util.JwtUtils;
import com.fxf.srb.core.hfb.RequestHelper;
import com.fxf.srb.core.pojo.vo.UserBindVo;
import com.fxf.srb.core.service.IUserBindService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author 饭小范
 * @version 1.0
 * @description: TODO
 * @date 2021/5/31 21:43
 */
@RestController
@Api("会员账号绑定")
@RequestMapping("/api/core/userBind")
@Slf4j
public class UserBindController {

    @Resource
    private IUserBindService iUserBindService;

    @ApiOperation("账户绑定提交数据")
    @PostMapping("/auth/bind")
    public R bind(@RequestBody UserBindVo userBindVO, HttpServletRequest request) {

        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        String formStr = iUserBindService.commitBindUser(userBindVO,userId);
        return R.ok().data("formStr",formStr);

    }

    @ApiOperation("账户绑定异步回调")
    @PostMapping("/notify")
    public String notify(HttpServletRequest request) {
        //从请求头中获取map参数集合
        Map<String, Object> paramMap = RequestHelper.switchMap(request.getParameterMap());
        log.info("用户账号绑定异步回调：" + JSON.toJSONString(paramMap));
        //校验签名
        if (RequestHelper.isSignEquals(paramMap)){
            log.error("用户账号绑定异步回调签名错误：" + JSON.toJSONString(paramMap));
            return "fail";
        }
        iUserBindService.notify(paramMap);
        return "success";

    }
}
